<template>
<el-config-provider :locale="locale">
<div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card >
        <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
           <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList" >
              <template #append>
                <el-button  :icon="Search" @click="getOrderList" />
              </template>
            </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="id" label="订单编号"  />
        <el-table-column prop="OrderState" label="订单状态"  >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.OrderState==='已支付'">已支付</el-tag>
            <el-tag type="warning" v-if="scope.row.OrderState==='待支付'">未支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="PaymentMethod" label="支付方式"  />
        <el-table-column prop="PaymentMoney" label="支付金额" />
        <el-table-column prop="PaymentTime" label="支付时间"  />
        <el-table-column  label="操作" >
          <!-- scope是一个插槽 -->
          <template #default="scope">  
            <!-- 编辑按钮 -->
            <el-button size="small" @click="showEditDialog( scope.row.id)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button size="small" type="danger" @click="deleteById( scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination  v-model:currentPage="queryInfo.pagenum" v-model:page-size="queryInfo.pagesize"
      :page-sizes="[2, 3, 5, 10]" :small="small" :disabled="disabled" :background="background"
      layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange"  @current-change="handleCurrentChange" />

    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog v-model="addressVisible" title="修改地址" width="50%" @close="addressDialogClosed">
      <!-- 表格内容主体区域 -->
      <el-form ref="addressFormRef" :model="addressForm" :rules="addressFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="收件人" prop="name">
          <el-input v-model="addressForm.address2"/>
        </el-form-item>
        <el-form-item label="省市/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"/>
        </el-form-item>
        <el-form-item label="电话号码" prop="mobile">
          <el-input v-model="addressForm.address2"/>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addressVisible = false">取消</el-button>
          <el-button type="primary" @click="addressVisible= false">确定</el-button>
        </span>
      </template>
    </el-dialog>
</div>
</el-config-provider>
  
</template>

<script>
import cityData from './citydata.js'
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { ArrowRight } from '@element-plus/icons-vue'
import { Search } from '@element-plus/icons-vue'
import qs from 'qs'
import {  ElMessageBox } from 'element-plus'
export default {
    components:{
    ArrowRight,
    ElConfigProvider,
    ElMessageBox

 },
  setup(){
    return{
      Search,
      locale: zhCn
    }
  },
  data(){
    //验证手机号的规则
    var checkTelNum=(rule,value,cb)=>{
      //验证手机号的正则表达式
      const regTelNum=/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if(regTelNum.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return{
      //获取项目列表的参数对象
      queryInfo:{
        query:'',
        //当前页数
        pagenum:1,
        //当前每页显示多少调数据
        pagesize:3,
        
      },
      orderList:[],
      total:0,
      //隐藏对话框
      addressVisible:false,
      addressForm:{
        name:'',
        address1:[],
        address2:'',
        mobile:'',
      },
      addressFormRules:{
        name:[{
          required:true,message:'请输入姓名',trigger:'blur'
        }],
        address1:[{
          required:true,message:'请选择省市区县',trigger:'blur'
        }],
        address2:[{
          required:true,message:'请填写详细地址',trigger:'blur'
        }],
        mobile:[{
          required:true,message:'请输入电话号码',trigger:'blur'
        },{validator:checkTelNum,trigger:'blur'}],
      },
      cityData,

    }
 },

  created(){
    this.getOrderList()

  },

  methods:{
    async getOrderList(){
      const {data:res} =await this.$http.get('/my/order/getorder',{params:this.queryInfo})
      if(res.meta.status!==200) {
        return this.$message.err('获取订单列表失败')
      }
      this.orderList=res.data.order
      this.total=res.data.total
        
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize){
      //console.log(newSize)
      this.queryInfo.pagesize=newSize
      this.getOrderList()

    },
    //监听页码值改变的事件
    handleCurrentChange(newPage){
      console.log(newPage)
      this.queryInfo.pagenum=newPage
      this.getOrderList()

    },
    //展示修改地址对话框
    showEditDialog(){
      this.addressVisible=true

    },
    //关闭对话框事件
    addressDialogClosed(){
      this.$refs.addressFormRef.resetFields()
    },

    //根据id删除订单信息
    async deleteById(id){
      //console.log(id)
      //弹框提示是否删除数据
      const confirmResult=await ElMessageBox.confirm('此操作将永久删除该订单，是否继续?','提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(err=>err)
      //如果用户确认删除，返回字符串confirm
      //console.log(confirmResult)
      //如果用户取消了删除，返回值为字符串cancel
      if(confirmResult!=='confirm'){
      return  this.$message.info('已取消删除')
      }
      const {data:res} =await this.$http.get('/my/order/deleteorder/'+id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除订单失败！')
      }
      this.$message.success('删除订单成功！')
    
    }

        
  }

}
</script >

<style lang="less" scoped>
.el-cascader{
  width: 100%;
}

</style>